﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Windows.Media.Imaging;
using System.IO;
using Microsoft.Xna.Framework.Media;
using System.Windows.Navigation;

namespace Nok_Air
{
    public partial class Finish : PhoneApplicationPage
    {
        public Finish()
        {
            InitializeComponent();
            DataContext = App.ViewModel;
            DatePicker dp = new DatePicker();
            DateTime dt = dp.Value.Value;            
            string date = "";
            if (App.lang == "eng")
            {
                date = String.Format("{0:dddd, MMMM d, yyyy}", dt) + " before 21.53";
            }
            else
            {
                string numDate = dt.Day.ToString(); //tmpDate[0];
                string day = dt.DayOfWeek.ToString();//tmpDate[1];
                string month = dt.Month.ToString();//tmpDate[2];
                int year = dt.Year + 543;

                switch (day.Substring(0, 3))
                {
                    case "Sun": day = "อาทิตย์"; break;
                    case "Mon": day = "จันทร์"; break;
                    case "Tue": day = "อังคาร"; break;
                    case "Wed": day = "พุธ"; break;
                    case "Thu": day = "พฤหัสบดี"; break;
                    case "Fri": day = "ศุกร์"; break;
                    case "Sat": day = "เสาร์"; break;
                }
                switch (month)
                {
                    case "1": month = "มกราคม"; break;
                    case "2": month = "กุมภาพันธ์"; break;
                    case "3": month = "มีนาคม"; break;
                    case "4": month = "เมษายน"; break;
                    case "5": month = "พฤษภาคม"; break;
                    case "6": month = "มิถุนายน"; break;
                    case "7": month = "กรกฎาคม"; break;
                    case "8": month = "สิงหาคม"; break;
                    case "9": month = "กันยายน"; break;
                    case "10": month = "ตุลาคม"; break;
                    case "11": month = "พฤศจิกายน"; break;
                    case "12": month = "ธันวาคม"; break;
                }

                date = numDate + " " + month + " " + year + " ไม่เกินเวลา 21.53";
            }
            App.ViewModel.WithinMsg = date;
        }

        private void continueButton_Click(object sender, RoutedEventArgs e)
        {          
          
            try
            {
                WriteableBitmap wb = new WriteableBitmap(LayoutRoot, null);
                MemoryStream stream = new MemoryStream((int)LayoutRoot.ActualHeight * (int)LayoutRoot.ActualWidth * 4);
                wb.SaveJpeg(stream, (int)LayoutRoot.ActualWidth, (int)LayoutRoot.ActualHeight, 0, 100);
                stream.Seek(0, 0);
                MediaLibrary ml = new MediaLibrary();
                ml.SavePicture("NokAirBookingNo_" + App.ViewModel.BookingItems.PAYMENTS.PaymentNo + ".jpg", stream);

                MessageBox.Show(App.ViewModel.BookingFinishAlertMsg + App.ViewModel.BookingItems.PAYMENTS.PaymentNo + "\".");

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            App.ViewModel.IsFinishTransaction = true;
            NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.RelativeOrAbsolute));
            //NavigationService.GoBack();
        }
    }
}